package com.congwulin.wechat.controller;

import com.congwulin.wechat.entity.common.CommonJSonData;
import com.congwulin.wechat.entity.common.SuperVO;
import com.congwulin.wechat.entity.order.OrderBillVO;
import com.congwulin.wechat.entity.order.OrderBodyVO;
import com.congwulin.wechat.entity.order.OrderHeadVO;
import com.congwulin.wechat.service.OrderService;
import com.congwulin.wechat.util.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
@RequestMapping(value = "order")
public class OrderController {
    @Autowired
    private OrderService service;

    @RequestMapping(value = "save")
    public @ResponseBody
    ReturnData save(@RequestBody Object obj){
        Map<String, Class<?>> map = new HashMap<String, Class<?>>();
        map.put("head", OrderHeadVO.class);
        map.put("body", OrderBodyVO.class);
        ArrayList<Map<Class<?>,List<SuperVO>>> voList =  BillTransUtil.ObjToEntity(obj, map);
        if(voList==null){
            ExceptionUtils.wrapBusinessException("保存的数据格式有误！！！");
        }
//        System.out.println(obj.toString());
        List<OrderBillVO> billvoList = new ArrayList<OrderBillVO>();
        OrderBillVO billVO = null;
        for(Map<Class<?>,List<SuperVO>> voMap: voList){
            billVO = new OrderBillVO();
            billVO.setHead(voMap.get(OrderHeadVO.class).get(0));
            billVO.setChildren(OrderBodyVO.class,voMap.get(OrderBodyVO.class));
            billvoList.add(billVO);
        }
        billvoList= service.save(billvoList);
        CommonJSonData data = BillTransJSonUtil.transJSon(billvoList);
        ReturnData returnData = new ReturnData(0,data,"订单保存成功！！！");
        return returnData;
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public @ResponseBody
    ReturnData list(@RequestBody PageSearch pageSearch){
        PageHelper.startPage(pageSearch.getPage(),pageSearch.getSize(),"ts desc");
        List<OrderHeadVO> list = service.getList(pageSearch.getSearchParams());
        ReturnData data = new ReturnData(0, list, "获取订单列表成功！！！");
        return data;
    }

    @RequestMapping(value = "bodylist", method = RequestMethod.POST)
    public @ResponseBody
    ReturnData bodylist(@RequestBody PageSearch pageSearch){
        PageHelper.startPage(pageSearch.getPage(),pageSearch.getSize(),"ts desc");
        List<OrderBodyVO> list = service.getBodyList(pageSearch.getSearchParams());
        ReturnData data = new ReturnData(0, list, "获取订单货品详细列表成功！！！");
        return data;
    }
}
